package com.ldc.common.mvvm;

import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.ViewModel;

import com.ldc.common.common.CommonResultCallback;
import com.ldc.common.exception.CommonException;
import com.ldc.common.model.BaseModel;
import com.ldc.common.net.Api2Request;
import com.ldc.common.net.BaseServer;
import com.ldc.common.utils.RxThreadUts;
import com.ldc.common.utils.ToastUtils;

import io.reactivex.Observable;
import io.reactivex.Single;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

public abstract class ViewModelBase extends ViewModel implements LifecycleEventObserver {
    protected CompositeDisposable compositeDisposable = new CompositeDisposable();
    private final BaseServer baseServer = Api2Request.getInstance().createServer(BaseServer.class);

    /**
     * 发起请求
     *
     * @param request  请求
     * @param callback 结果回调
     * @param <T>      泛型
     */
    public <T> void apiRequest(Single<BaseModel<T>> request, CommonResultCallback<Boolean, BaseModel<T>, String> callback) {
        if (null == request) {
            return;
        }
        Disposable disposable = request.compose(RxThreadUts.singleTransformer())
                .subscribe(rt -> {
                    if (null != rt) {
                        callback.onResult(true, rt, "");
                        if (rt.isFailed()) {
                            ToastUtils.showShort(rt.getErrorMsg());
                        }
                    } else {
                        throw new CommonException("result is null");
                    }
                }, throwable -> {
                    callback.onResult(false, null, throwable.getMessage());
                });

        compositeDisposable.add(disposable);
    }

    /**
     * 发起请求
     *
     * @param request  请求
     * @param callback 结果回调
     * @param <T>      泛型
     */
    public <T> void apiRequest(Observable<BaseModel<T>> request, CommonResultCallback<Boolean, BaseModel<T>, String> callback) {
        if (null == request) {
            return;
        }
        Disposable disposable = request.compose(RxThreadUts.observableTransformer())
                .subscribe(rt -> {
                    if (null != rt) {
                        callback.onResult(true, rt, "");
                        if (rt.isFailed()) {
                            ToastUtils.showShort(rt.getErrorMsg());
                        }
                    } else {
                        throw new CommonException("result is null");
                    }
                }, throwable -> {
                    callback.onResult(false, null, throwable.getMessage());
                });

        compositeDisposable.add(disposable);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        compositeDisposable.dispose();
    }

    @Override
    public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
        switch (event) {
            case ON_START:
                onStart();
                break;
            case ON_CREATE:
                onCreate();
                break;
            case ON_RESUME:
                onResume();
                break;
            case ON_PAUSE:
                onPause();
                break;
            case ON_STOP:
                onStop();
                break;
            case ON_DESTROY:
                onDestroy();
                break;
            default:
                //  There is no other action
                break;

        }
    }


    private void selectCollect(String id) {
        apiRequest(baseServer.select_collect(id), (status, result, throwable) -> {
            if (result.isSuccess()) {
                ToastUtils.showShort(result.getErrorMsg());
            }
        });
    }

    private void unSelectCollect(String id) {
        apiRequest(baseServer.un_select_collect(id), (status, result, throwable) -> {
            if (result.isSuccess()) {
                ToastUtils.showShort(result.getErrorMsg());
            }
        });
    }

    private void unSelectCollectOriginId(String id, String originId) {
        apiRequest(baseServer.un_select_collect_originId(id, originId), (status, result, throwable) -> {
            if (result.isSuccess()) {
                ToastUtils.showShort(result.getErrorMsg());
            }
        });
    }


    protected void onStart() {
    }

    protected void onCreate() {
    }

    protected void onResume() {
    }

    protected void onPause() {
    }

    protected void onStop() {
    }

    protected void onDestroy() {
    }
}
